<?php


namespace app\game;


use app\game\node\AbsNode;
use app\game\node\NodeFood;

class FoodList
{
    protected $foodList;

    public function add(AbsNode $food)
    {
        $this->foodList[] = $food;
    }

    /**
     * 获取第一个食物
     * @return AbsNode
     */
    public function getFirstFood()
    {
        return $this->foodList[0];
    }

    public function remove(AbsNode $food)
    {
        $tempFoodList = [];
        foreach ($this->foodList as $key => $item)
        {
            if ($food === $item)
            {
                unset($this->foodList[$key]);
            }else{
                $tempFoodList[] = $item;
            }
        }

        $this->foodList = $tempFoodList;
    }

    public function createFood(RandRowCol $randRowCol,Form $form)
    {
        if ($this->isEmpty()){

            $point = $randRowCol->createRowCol();
            $food = new NodeFood($point[0],$point[1]);
            $form->setRowColNode($point[0], $point[1], $food);

            $this->foodList[] = $food;
        }
    }

    public function isEmpty()
    {
        return empty($this->foodList);
    }
}